<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>01_列表选项的操作</title>
  <style>
    .active {
      color: red;
    }

    table {
      border-collapse: collapse;
    }

    thead {
      background-color: #f5f5f5;
    }

    td, th {
      text-align: center;
      border: 1px solid #ccc;
      padding: 10px 20px;
    }
    
    .bg-active {
      background-color: skyblue;
    }
  </style>
</head>
<body>

  <div id="app">
    <h2>1. 列表选中</h2>
    <ul>
      <template v-for="(item, index) in movies" :key="item">
        <li :class="{ active: index === currentIndex }" @click="toggle(index)">{{ item }}</li>
      </template>
    </ul>

    <h2>2. 购物车列表操作</h2>
    <table>

      <thead>
        <tr>
          <th>编号</th>
          <th>名称</th>
          <th>价格</th>
          <th>数量</th>
          <th>操作</th>
        </tr>
      </thead>

      <tbody>
        <template v-for="(item, index) in cart" :key="item.id">
          <tr :class="{ 'bg-active': index === cartCurrentIndex }" @click="rowClick(index)">
            <td>{{ item.id }}</td>
            <td>{{ item.name }}</td>
            <td>{{ formatPrice(item.price) }}</td>
            <td>
              <button @click="increment(index)">+</button>
              {{ item.count }}
              <button @click="decrement(index)" :disabled="item.count <= 1">-</button>
            </td>
            <td><button @click="remove(index)">移除</button></td>
          </tr>
        </template>
        
      </tbody>

    </table>
    <h3>总价: {{ formatPrice(totalPrice) }}</h3>

  </div>

  <script src="../lib/vue.js"></script>
  <script src="./data/data.js"></script>
  <script>

    const { createApp, ref, reactive, computed } = Vue

    createApp({
      setup() {
        const currentIndex = ref(-1)
        const cartCurrentIndex = ref(-1)
        const movies = ref(["星际穿越", "流浪地球", "复仇者联盟", "哪吒之魔童降世"])
        const cart = ref(books)

        const toggle = (index) => {
          currentIndex.value = index
        }

        const formatPrice = (price) => {
          return "¥" + price
        }

        const increment = (index, count = 1) => {
          cart.value[index].count += count
        }

        const decrement = (index, count = 1) => {
          if (cart.value[index].count <= 1) {
            return
          }
          cart.value[index].count -= count
        }

        const remove = (index) => {
          cart.value.splice(index, 1)
        }

        const rowClick = (index) => {
          cartCurrentIndex.value = index
        }

        const totalPrice = computed(() => {
          return cart.value.reduce((total, item) => {
            return total + item.count * item.price
          }, 0)
        })

        return {
          currentIndex,
          movies,
          toggle,

          cart,
          cartCurrentIndex,
          totalPrice,
          formatPrice,
          increment,
          decrement,
          remove,
          rowClick
        }
      }
    }).mount('#app')

  </script>
  
</body>
</html>